#include <iostream>
#include <unistd.h>
#include "ShareMemory.hpp"
#include "Time.hpp"
#include "Fifo.hpp"

int main()
{
    // 删除key值为0x00000000的共享内存块
    //  int res = 0;
    //  int shmid[2] = {23, 25};
    //  struct shmid_ds buff; //struct shmid_ds是系统提供的类型
    //  for(int i = 0; i < 2; i++)
    //  {
    //      res = shmctl(shmid[i], IPC_STAT, &buff); //IPC_STAT选项是将共享内存的状态保存到buffer中
    //      if(res == -1)
    //      {
    //          std::cerr << "shmctl error" << std::endl;
    //          return -1;
    //      }
    //      std::cout << "shm_cpid: " << buff.shm_cpid << std::endl;
    //      std::cout << "shm_lpid: " << buff.shm_lpid << std::endl;
    //  }

    //std::cout << "current time: " << GetCurrTime() << std::endl;

    // 1.创建共享内存
    shm.CreateShm();
    // 2.将共享内存与进程地址空间挂接
    shm.AttachShm();

    gfifo.OpenPipeForRead();

    struct data *image = (struct data *)shm.GetAddr();
    shm.ShmMeta();
    
    while (true)
    {
        gfifo.Wait(); //利用命名管道实现共享内存的保护机制

        printf("status: %s\n", image->status);
        printf("lasttime: %s\n", image->lasttime);
        printf("image: %s\n", image->image);
    }

    // 3.将共享内存与进程地址空间去关联
    shm.DetachShm();
    // 4.手动删除共享内存
    shm.DeleteShm();

    return 0;
}